//
//  CommonHandtitleView.m
//  jujiaxin
//
//  Created by 刘应平 on 16/11/17.
//  Copyright © 2016年 刘应平. All rights reserved.
//

#import "CommonHandtitleView.h"
#define BUTTONTAG 200
@interface CommonHandtitleView ()
{
    CGFloat _heandTitleWidth;
    CGFloat _headTitleHeight;
    UIButton*_lastButton;
    NSInteger _choose;
}

@property (nonatomic, strong) UIView *underLineView;


@end
@implementation CommonHandtitleView

- (instancetype)initWithFrame:(CGRect)frame handTitles:(NSArray *)handtitles choose:(NSInteger)choose
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.handTitles = handtitles;
        _choose = choose;
        _headTitleHeight = frame.size.height;
        _heandTitleWidth = [UIScreen zj_screenWith] / self.handTitles.count;
        [self getUI];
    }
    return self;
}



#pragma mark - 界面
- (void)getUI{
    
    [self.handTitles enumerateObjectsUsingBlock:^(NSString  *_Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(_heandTitleWidth * 0.1 + _heandTitleWidth * idx, 10 * [UIScreen zj_screenHScale], _heandTitleWidth  * 0.8, 30 * [UIScreen zj_screenHScale])];
        [button setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor themeColor] forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont systemFontOfSize:16 * [UIScreen zj_textFontScale]];
        [button setTitle:title forState:UIControlStateNormal];
        
        [button setTag:idx + BUTTONTAG];
        [button addTarget:self action:@selector(commonHand_button:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        
    /**  刚刚进来显示第一个  */
        if (idx == _choose) {
            [self layoutButtonWithIndex:idx];
        }
    }];
    
    TenGrayShadowView *backView = [[TenGrayShadowView alloc] initWithFrame:CGRectMake( 0 , _headTitleHeight - 1 * [UIScreen zj_screenHScale], [UIScreen zj_screenWith], 1 * [UIScreen zj_screenHScale])];
    backView.backgroundColor = [UIColor themeGrayColor];
    [self addSubview:backView];
    
    self.underLineView = [[UIView alloc] initWithFrame:CGRectMake( _heandTitleWidth * 0.3 + _heandTitleWidth * _choose , 48 * [UIScreen zj_screenHScale], _heandTitleWidth * 0.4, 2 * [UIScreen zj_screenHScale])];
    self.underLineView.backgroundColor = [UIColor themeColor];
    [self addSubview:self.underLineView];
    
   
    

    
}


#pragma mark - button的点击的状态的改变
/**  布局  */
- (void)layoutButtonWithIndex:(NSInteger)index{
    
    if (_lastButton) {
        _lastButton.selected = NO;
        
    }
    UIButton *button = (UIButton *)[self viewWithTag:index + BUTTONTAG];
   
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame  = self.underLineView.frame;
        frame.origin.x =_heandTitleWidth * 0.3 + _heandTitleWidth * index;
        self.underLineView.frame = frame;
        button.selected = YES;
    }];
    _lastButton = button;
    
}


#pragma mark - 监听事件
- (void)commonHand_button:(UIButton *)commonHandButton{
    
    /**  布局  */
    [self layoutButtonWithIndex:commonHandButton.tag - BUTTONTAG];
    /**  判断协议存在，执行协议方法  */
    if (self.commonHandDelegate && [self.commonHandDelegate respondsToSelector:@selector(titleScrollView:buttonTag:)]) {
        [self.commonHandDelegate titleScrollView:self buttonTag:commonHandButton.tag - BUTTONTAG] ;
    }
    

}
#pragma mark - 协议代理


#pragma mark - getter

@end
